package com.hejia.alauda.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import static org.apache.commons.lang3.time.DateFormatUtils.ISO_DATE_FORMAT;
import static org.apache.commons.lang3.time.DateUtils.isSameDay;
import static org.apache.commons.lang3.time.DateUtils.addDays;
import static org.springframework.util.StringUtils.quote;
/**
 * 里面存放计算利率、利息等的工具类
 * @author paul
 *
 */
public class RateUtil {
	
	protected final static Log logger = LogFactory.getLog(RateUtil.class);
	
	public static final String DEFAULT_DATE = ISO_DATE_FORMAT.format(new Date(0));
	public static final String DEFAULT_DATE_STR = "购买成功后即日开始计算收益";
	
	//public static String FINANCING_ORDER_STR = null;
	
	public static String getFinancingOrder(Date date) {
		String str = Utils.formatDate(date);
		StringBuilder sb = new StringBuilder();
		sb.append("CASE ");
		sb.append("WHEN `status` = \'STATUS_REPAID\' THEN 10 ");
		sb.append("WHEN `status` = \'STATUS_PAYBACK\' THEN 9 ");
		sb.append("WHEN `status` = \'STATUS_EXPIRED\' THEN 8 ");
		sb.append("WHEN DATE_FORMAT(`settlementDate`, \'%Y-%m-%d\') <= " + quote(str) + " THEN 8 ");
		sb.append("ELSE (CASE WHEN (SELECT product.category FROM product WHERE product.id = financing.product_id) = \'CURRENT\' THEN (");
		sb.append("CASE WHEN (SELECT COUNT(id) FROM redemptionrequest WHERE redemptionrequest.financing_id = financing.id) > 0 THEN 6 ");
		sb.append("ELSE (CASE WHEN (SELECT COUNT(productearningsrule.id) FROM productearningsrule WHERE " + quote(str) + " BETWEEN DATE_FORMAT(productearningsrule.redemptionStartTime, \'%Y-%m-%d\') AND DATE_FORMAT(productearningsrule.redemptionEndTime, \'%Y-%m-%d\') AND productearningsrule.product_id=financing.product_id) > 0 THEN 1 ");
		sb.append("ELSE 4 END) END) ELSE 4 END) END");
		
		return sb.toString();
	}
	/**
	 * 计算利息，精确到分
	 * @param amout
	 * @param rate
	 * @param term
	 * @return
	 */
	public static BigDecimal calculateInterestPerDiem(BigDecimal amount,BigDecimal rate,Integer cycleDays, Integer days){
		double interestPerDiem = 0;
		if(amount!=null && rate!=null && cycleDays!=null && days!=null){
			interestPerDiem = amount.doubleValue()*rate.doubleValue()/cycleDays*days;
		}
		return new BigDecimal(interestPerDiem).setScale(2, RoundingMode.UP);
	}
	
	public static Date calculateSettlementDate(Date valueDate,Integer term){
		if (valueDate != null && term != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(valueDate);
			int day = calendar.get(Calendar.DAY_OF_YEAR);
			calendar.set(Calendar.DAY_OF_YEAR, day + term);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			return calendar.getTime();
		}
		return null;
	}
	
	public static int calculateDate(Date date1,Date date2){
		int count = 0;
		if (date1 != null && date2 != null && !isSameDay(date1,date2)) {
			if(date1.getTime() < date2.getTime()){
				while(!isSameDay(addDays(date1,count), date2)){
					count++;
//					logger.info("count:" + count);
				}
			} else {
				while(!isSameDay(addDays(date1,count), date2)){
					count--;
//					logger.info("count" + count);
				}
			}
		}
		return count;
	}
	
	public static Date calculateValueDate(Date date) {
		Date now = new Date();
		return date != null ? (ISO_DATE_FORMAT.format(now).compareTo(
				ISO_DATE_FORMAT.format(date)) >= 0 ? now : date) : now;
	}
	
	public static String calculateValueDateStr(Date date) {
		Date now = new Date();
		return date != null ? (ISO_DATE_FORMAT.format(now).compareTo(
				ISO_DATE_FORMAT.format(date)) >= 0 ? DEFAULT_DATE_STR
				: ISO_DATE_FORMAT.format(date)) : DEFAULT_DATE_STR;
	}
	
	public static Date getDate() {
		return DateUtils.setMilliseconds(new Date(), 0);
	}
	
	public static BigDecimal getStockIncrease(BigDecimal b1, BigDecimal b2){
		return b2.subtract(b1).divide(b1, 4, RoundingMode.UP);
	}
	
	public static void main(String[] args) {
		Date now = new Date();
		System.out.println(calculateDate(now, addDays(now, -2)));
	}
}
